/*******************************************************************************
 * Copyright (c) 2008-2010 Cedric Vidal
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Cedric Vidal - original idea, initial contribution and API
 *******************************************************************************/

package org.trans4j.impl;

import java.lang.reflect.Method;

import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

/**
 * @author <a href="mailto:c.vidal@proxiad.com">Cedric Vidal, ProxiAD Group</a>
 * 
 */
public abstract class AbstractDelegateMethodInterceptor implements
		MethodInterceptor {

	public MethodInterceptor getDelegate() {
		return delegate;
	}

	public void setDelegate(MethodInterceptor delegate) {
		this.delegate = delegate;
	}

	private MethodInterceptor delegate = null;

	protected Object invokeDelegateOrSuper(Object target, Method method,
			Object[] params, MethodProxy proxy) throws Throwable {
		Object result = null;
		if (delegate != null) {
			result = delegate.intercept(target, method, params, proxy);
		} else {
			result = proxy.invokeSuper(target, params);
		}
		return result;
	}
}
